#include "metal.h"


Metal::Metal()
{
    metalType = "M1";
    this->setFlag(QGraphicsItem::ItemIsMovable);
    contactMetal = false;
    gateM1 = false;
    linkedTransistors = new QList<void*>();
    linkedMetal = new QList<Metal*>();
    drawNetName = true;
    porpouse = 0;
    band = "NONE";
}

void Metal::copy(Metal *m)
{
    this->bottomRight = m->bottomRight;
    this->metalType = m->metalType;
    this->scale = m->scale;
    this->net = m->net;
    this->drawNetName = m->drawNetName;
    this->contactMetal = m->contactMetal;
    this->gateM1 = m->gateM1;
    this->band = m->band;
    this->linkedTransistors->append(*(m->linkedTransistors));
    this->linkedMetal->append(*(m->linkedMetal));
    this->setPos(m->pos());
}

void Metal::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QBrush brush;
    QPen pen;
    QTransform tr;
    tr.scale(1/(*scale),1/(*scale));
    brush.setTransform(tr);
    pen.setWidth(0);

    if(this->metalType.compare("M1",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(QColor(0,0X4F,0XFF));
        brush.setStyle(Qt::FDiagPattern);
        pen.setColor(QColor(0,0,0XFF));
        this->setZValue(1);
    }
    else if(this->metalType.compare("M2",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(Qt::white);
        brush.setStyle(Qt::FDiagPattern);
        pen.setColor(Qt::white);
        this->setZValue(1);
    }
    else if(this->metalType.compare("M1PIN",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(Qt::transparent);
        pen.setColor(QColor(0,0,0XFF));

        this->setZValue(1);
    }
    else if(this->metalType.compare("PPLUS",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(Qt::yellow);
        brush.setStyle(Qt::Dense7Pattern);
        pen.setColor(Qt::white);
        painter->setOpacity(0.5);

        this->setZValue(-1);
    }
    else if(this->metalType.compare("NWELL",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(Qt::transparent);
        pen.setColor(QColor(0XFF,0X66,0X33));

        this->setZValue(-1);
    }
    else if(this->metalType.compare("NPLUS",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(Qt::white);

        brush.setStyle(Qt::Dense7Pattern);
        pen.setColor(Qt::yellow);
        painter->setOpacity(0.5);

        this->setZValue(-1);
    }
    else if(this->metalType.compare("PO",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(QColor(0XFF,0,0));
        brush.setStyle(Qt::BDiagPattern);
        pen.setColor(QColor(0XFF,0,0));
    }
    else if(this->metalType.compare("OD",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(QColor(0,0XCC,0x66));
        brush.setStyle(Qt::Dense6Pattern);
        pen.setColor(QColor(0,0XCC,0x66));
    }
    else if(this->metalType.compare("CO",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(Qt::transparent);
        brush.setStyle(Qt::SolidPattern);
        pen.setColor(QColor(0XFF,0xFF,0));

        painter->setBrush(brush);
        painter->setPen(pen);

        painter->drawLine(boundingRect().topLeft(),boundingRect().bottomRight());
        painter->drawLine(boundingRect().bottomLeft(),boundingRect().topRight());
    }
    painter->setBrush(brush);
    painter->setPen(pen);

    painter->drawRect(this->boundingRect());
    painter->setOpacity(1);

    if(drawNetName)
    {
        QFont font;
        font.setPixelSize(3);
        pen.setColor(Qt::white);
        painter->setFont(font);
        painter->setPen(pen);
        painter->drawText(this->boundingRect(),Qt::AlignCenter,this->net);
    }
}

QRectF Metal::boundingRect() const
{
    return QRectF(0,0,bottomRight.x(),bottomRight.y());
}

void Metal::setMetalType(QString newType)
{
    this->metalType = newType;
}

void Metal::clear()
{
    bottomRight.setX(0);
    bottomRight.setY(0);
}

QString Metal::getCoordinates()
{
    QPointF bottomLeft = mapToScene(boundingRect().bottomLeft());
    QPointF bottomRight = mapToScene(boundingRect().bottomRight());
    QPointF topRight = mapToScene(boundingRect().topRight());
    QPointF topLeft = mapToScene(boundingRect().topLeft());

    QString p;

    p = QString::number(bottomLeft.x()*10) + "," + QString::number(bottomLeft.y()*10);
    p += " " + QString::number(bottomRight.x()*10) + "," + QString::number(bottomRight.y()*10);
    p += " " + QString::number(topRight.x()*10) + "," + QString::number(topRight.y()*10);
    p += " " + QString::number(topLeft.x()*10) + "," + QString::number(topLeft.y()*10) + ";";

    return p;
}
